public class Demo1 {

    public static int sum1=0;
    public static int sum2=0;
    public static void main(String[] args) throws InterruptedException {
        long beg=System.currentTimeMillis();
        int[] arr=new int[1000];
        for(int i=0;i<arr.length;i++) {
            arr[i]=i+1;
        }
        Thread t1=new Thread(()-> {
            for(int i=0;i<arr.length;i+=2) {
                sum1+=arr[i];
            }
        });
        Thread t2=new Thread(()-> {
            for(int i=1;i<arr.length;i+=2) {
                sum2+=arr[i];
            }
        });
        t1.start();
        t2.start();
        t1.join();//定义两个变量,分别累加,t1往sum1累加,t2往sum2累加,那么就不会出现安全问题了
        t2.join();

        System.out.println("sum1="+sum1);
        System.out.println("sum2="+sum2);
        System.out.println("sum="+(sum1+sum2));
        long end=System.currentTimeMillis();
        System.out.println("程序运行时间="+(end-beg)+"毫秒");
    }
}
